1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Drawing;
5 using System.Data;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9 using System.Windows.Forms;
10 using System.Data.SqlClient;
11
12
13 namespace SoftQuanLyNhaHang.Views
14 {
15 public partial class uctNhanVien : UserControl
16 {
17 public uctNhanVien()
18 {
19 InitializeComponent();
20 }
21
22 public static uctNhanVien uctNV = new uctNhanVien();
23 int flag = 0;
24
25 public void HienThiDanhSachNhanVien()
26 {
27 dgvDanhSachNV.DataSource = Models.NhanVienMod.FillDataSetNhanVien().Tables[0];
28 dgvDanhSachNV.Dock = DockStyle.Fill;
29 dgvDanhSachNV.RowHeadersVisible = false;
30 dgvDanhSachNV.BorderStyle = BorderStyle.Fixed3D;
31 }
32 // Resize the master DataGridView columns to fit the newly loaded data. masterDataGridView.AutoResizeColumns();
33 // Configure the details DataGridView so that its columns automatically
34 // adjust their widths when the data changes.
35 //private void SizeAllColumns(Object sender, EventArgs e)
36 //{
37 // dgvDanhSachNV.AutoResizeColumns(
38 // DataGridViewAutoSizeColumnsMode.AllCells);
39 // dgvDanhSachNV.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
40
41 //}
42 private void uctNhanVien_Load(object sender, EventArgs e)
43 {
44 dis_end(false);
45 HienThiDanhSachNhanVien();
46 bingding();
47 }
48 public void nhung(Control ctr)
49 {
50 pnlDanhSachNV.Controls.Clear();
51 pnlDanhSachNV.BorderStyle = BorderStyle.Fixed3D;
52 ctr.Dock = DockStyle.Fill;
53 pnlDanhSachNV.Controls.Add(ctr);
54 pnlDanhSachNV.Show();
55 }
56 void loadcontrol()
57 {
58 cmbGioiTinhNV.Items.Clear();
59 cmbGioiTinhNV.Items.Add("Nam");
60 cmbGioiTinhNV.Items.Add("Nữ");
61 }
62 void bingding()
63 {
64 txtIdNhanVien.DataBindings.Clear();
65 txtIdNhanVien.DataBindings.Add("Text", dgvDanhSachNV.DataSource, "IdNhanVien");
66 txtHolotNV.DataBindings.Clear();
67 txtHolotNV.DataBindings.Add("Text", dgvDanhSachNV.DataSource, "HoLot");
68 txtTenNV.DataBindings.Clear();
69 txtTenNV.DataBindings.Add("Text", dgvDanhSachNV.DataSource, "Ten");
70 dtpNgaySinhNV.DataBindings.Clear();
71 dtpNgaySinhNV.DataBindings.Add("Text", dgvDanhSachNV.DataSource, "Ngaysinh");
72 cmbGioiTinhNV.DataBindings.Clear();
73 cmbGioiTinhNV.DataBindings.Add("Text", dgvDanhSachNV.DataSource, "GioiTinh");
74 txtDienThoaiNV.DataBindings.Clear();
75 txtDienThoaiNV.DataBindings.Add("Text", dgvDanhSachNV.DataSource, "DienThoai");
76 txtEmailNV.DataBindings.Clear();
77 txtEmailNV.DataBindings.Add("Text", dgvDanhSachNV.DataSource, "Email");
78 txtDiaChiNV.DataBindings.Clear();
79 txtDiaChiNV.DataBindings.Add("Text", dgvDanhSachNV.DataSource, "DiaChi");
80
81 }
82 //private void dgvDanhSachNV_Click(object sender, EventArgs e)
83 //{
84 // DataSet ds = new DataSet();
85 // try
86 // {
87 // //int i = 0;
88 // //i = dgvDanhSachNV.CurrentRow.Index;
89 // //string _idHocVien = dgvDanhSachNV.Rows[i].Cells[0].Value.ToString();
90 // //ds = Controllers.NhanVienCtrl.FillDataSet_getNhanVienByIdNhanVien(_idHocVien);
91 // //txtIdNhanVien.DataBindings.Clear();
92 // //txtIdNhanVien.DataBindings.Add("text", ds.Tables[0], "IdNhanVien");
93 // //txtHolotNV.DataBindings.Clear();
94 // //txtHolotNV.DataBindings.Add("text", ds.Tables[0], "HoLot");
95 // //txtTenNV.DataBindings.Clear();
96 // //txtTenNV.DataBindings.Add("text", ds.Tables[0], "Ten");
97 // //dtpNgaySinhNV.DataBindings.Clear();
98 // //dtpNgaySinhNV.DataBindings.Add("text", ds.Tables[0], "Ngaysinh");
99 // //cmbGioiTinhNV.DataBindings.Clear();
100 // //cmbGioiTinhNV.DataBindings.Add("text", ds.Tables[0], "GioiTinh");
101 // //txtDienThoaiNV.DataBindings.Clear();
102 // //txtDienThoaiNV.DataBindings.Add("text", ds.Tables[0], "DienThoai");
103 // //txtEmailNV.DataBindings.Clear();
104 // //txtEmailNV.DataBindings.Add("text", ds.Tables[0], "Email");
105 // //txtDiaChiNV.DataBindings.Clear();
106 // //txtDiaChiNV.DataBindings.Add("text", ds.Tables[0], "DiaChi");
107
108 // }
109 // catch { }
110
111 //}
112 void clearData()
113 {
114 txtIdNhanVien.Text = Models.connection.ExcuteScalar(String.Format("select IdNhanVien= dbo.fcgetIdNhanVien()"));
115 txtHolotNV.Text = "";
116 txtTenNV.Text = "";
117 txtDienThoaiNV.Text = "";
118 txtEmailNV.Text = "";
119 txtDiaChiNV.Text = "";
120 loadcontrol();
121 }
122 void dis_end(bool e)
123 {
124 //txtIdNhanVien.Enabled = e;
125 txtHolotNV.Enabled = e;
126 txtTenNV.Enabled = e;
127 dtpNgaySinhNV.Enabled = e;
128 cmbGioiTinhNV.Enabled = e;
129 txtDienThoaiNV.Enabled = e;
130 txtEmailNV.Enabled = e;
131 txtDiaChiNV.Enabled = e;
132 btnLuu.Enabled = e;
133 btnHuy.Enabled = e;
134 btnThemMoi.Enabled = !e;
135 btnXoa.Enabled = !e;
136 btnSua.Enabled = !e;
137 }
138 private void txtDienThoaiNV_KeyPress(object sender, KeyPressEventArgs e)
139 {
140 if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
141 {
142 e.Handled = true;
143 }
144 }
145
146 private void btnFind_Click(object sender, EventArgs e)
147 {
148 uctSearchNhanVien uctS = new uctSearchNhanVien();
149 nhung(uctS);
150 }
151
152 private void btnHide_Click(object sender, EventArgs e)
153 {
154 pnlDanhSachNV.Controls.Clear();
155 pnlDanhSachNV.BorderStyle = BorderStyle.None;
156 }
157
158 private void btnLuu_Click(object sender, EventArgs e)
159 {
160 string _idNhanVien = "";
161 try
162 {
163 _idNhanVien = txtIdNhanVien.Text;
164 }
165 catch { }
166
167 string _hoNhanVien = "";
168 try
169 {
170 _hoNhanVien = txtHolotNV.Text;
171 }
172 catch { }
173 string _tenNhanVien = "";
174 try
175 {
176 _tenNhanVien = txtTenNV.Text;
177 }
178 catch { }
179
180 DateTime _ngaysinhNhanVien = dtpNgaySinhNV.Value;
181
182 try { }
183 catch { }
184 string _giotinhNhanVien = "";
185 try
186 {
187 _giotinhNhanVien = cmbGioiTinhNV.Text;
188 }
189 catch { }
190 string _dienthoaiNhanVien = "";
191 try
192 {
193 _dienthoaiNhanVien = txtDienThoaiNV.Text;
194 }
195 catch { }
196 string _emailNhanVien = "";
197 try
198 {
199 _emailNhanVien = txtEmailNV.Text;
200 }
201 catch { }
202 string _diachiNhanVien = "";
203 try
204 {
205 _diachiNhanVien = txtDiaChiNV.Text;
206 }
207 catch { }
208 if (flag == 0)
209 {
210
211
212 if (_idNhanVien == "" || _tenNhanVien == "" || _hoNhanVien == "")
213 MessageBox.Show("Hãy nhập đầy đủ thông tin");
214 else
215 {
216 int i = 0;
217 i = Controllers.NhanVienCtrl.InSertNhanVien(_idNhanVien, _hoNhanVien, _tenNhanVien, _ngaysinhNhanVien, _giotinhNhanVien, _dienthoaiNhanVien, _emailNhanVien, _diachiNhanVien);
218 if (i > 0)
219 {
220 MessageBox.Show("Thêm mới thành công");
221 HienThiDanhSachNhanVien();
222 }
223 else
224 MessageBox.Show("Thêm mới không thành công");
225 }
226 }
227 else
228 {
229 int i = 0;
230 i = Controllers.NhanVienCtrl.UpdateNhanVien(_idNhanVien, _hoNhanVien, _tenNhanVien, _ngaysinhNhanVien, _giotinhNhanVien, _dienthoaiNhanVien, _emailNhanVien, _diachiNhanVien);
231 if (i > 0)
232 {
233 MessageBox.Show(" Sửa thành công");
234 HienThiDanhSachNhanVien();
235 uctNhanVien_Load(sender, e);
236 }
237 else
238 MessageBox.Show("Sửa không thành công");
239 }
240 uctNhanVien_Load(sender, e);
241 }
242
243 private void btnThemMoi_Click(object sender, EventArgs e)
244 {
245 flag = 0;
246 clearData();
247 dis_end(true);
248 }
249
250 private void btnHuy_Click(object sender, EventArgs e)
251 {
252 uctNhanVien_Load(sender, e);
253 dis_end(false);
254 }
255
256 private void btnSua_Click(object sender, EventArgs e)
257 {
258 flag = 1;
259 dis_end(true);
260 loadcontrol();
261 }
262
263 private void btnXoa_Click(object sender, EventArgs e)
264 {
265 string _idNhanVien = "";
266 try
267 {
268 _idNhanVien = txtIdNhanVien.Text;
269 }
270 catch { }
271 DialogResult dr = MessageBox.Show("Bạn có chắc chắn xóa ?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
272 if (dr == DialogResult.Yes)
273 {
274 int i = 0;
275 i = Controllers.NhanVienCtrl.DeleteNhanVien(_idNhanVien);
276 if (i > 0)
277 {
278 MessageBox.Show(" Xóa thành công");
279 HienThiDanhSachNhanVien();
280 uctNhanVien_Load(sender, e);
281 }
282 else
283 MessageBox.Show("Xóa không thành công");
284 }
285 else
286 return;
287 }
288
289 private void dgvDanhSachNV_CellContentClick(object sender, DataGridViewCellEventArgs e)
290 {
291
292 }
293
294
295 }
296 }